package org.altervista.cp.microkernel;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


public class BootstrapContext extends InitialContext {

	public BootstrapContext() throws NamingException {
		super(getEnv());
	}

	public Object lookup(String name) {
		try {
			return super.lookup(name);
		} catch (NamingException e) {
			return null;
		}
	}

	private static Environment getEnv() {
		Environment env = new Environment();
		env.put(INITIAL_CONTEXT_FACTORY, BootstrapProperties
				.getJNDIInitialContextFactory());
		env.put(Context.PROVIDER_URL, BootstrapProperties.getJNDIProviderUrl());
		env.put(Context.SECURITY_PRINCIPAL, BootstrapProperties
				.getJNDISecurityPrincipal());
		env.put(Context.SECURITY_CREDENTIALS, BootstrapProperties
				.getJNDISecurityCredentials());
		return env;
	}

	private static class Environment extends Hashtable<String, String> {
		private static final long serialVersionUID = 1L;

		public synchronized String put(String key, String value) {
			if (value == null) {
				return null;
			} else {
				return super.put(key, value);
			}
		}

	}
}
